<!--
Copyright 2009, Gregg Tavares.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

    * Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
    * Neither the name of Gregg Tavaresc. nor the names of his
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-->
<!--
Example of multiple "windows".
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>
Windows
</title>
<style>
  html, body {
    border: 0;
    margin: 0;
    height: 100%;
    height: 100%;
    text-align: center;
    }
</style>
<!-- Include sample javascript library functions-->
<script type="text/javascript" src="o3djs/base.js"></script>
<script type="text/javascript" src="gui/windows.js"></script>
<script type="text/javascript" src="apps/console.js"></script>
<script type="text/javascript" src="apps/spinning-cube.js"></script>
<script type="text/javascript" src="apps/instance-override.js"></script>
<script type="text/javascript" src="apps/simple-viewer.js"></script>

<!-- Our javascript code -->
<script type="text/javascript">
o3djs.require('o3djs.util');
o3djs.require('o3djs.math');

// Events
// init() once the page has finished loading.
// uninit() when the page is unloaded.
window.onload = init;
window.onunload = uninit;

// global variables
var g_o3d;
var g_math;
var g_client;
var g_pack;

function getURLFromPath(path) {
  var filePath = window.location.href;
  var index = filePath.lastIndexOf('/');
  filePath = filePath.substring(0, index + 1) + path;
  return filePath;
}

/**
 * Creates the client area.
 */
function init() {
  o3djs.util.makeClients(initStep2);
}

/**
 * Initializes o3d and loads the model into the transform graph.
 */
function initStep2(clientElements) {
  o3djs.dump.dump ("============ START =============\n");

  // Initializes global variables and libraries.
  var o3dElement = clientElements[0];
  g_o3d = o3dElement.o3d;
  g_math = o3djs.math;
  g_client = o3dElement.client;

  // Initialize the sample javascript library.
  o3djs.base.init(o3dElement);

  // Creates a pack to manage our resources/assets
  g_pack = g_client.createPack();

  var ui = createUI(o3dElement);

  ui.createCanvasWindow({ title: "red",
                          rect: createRect(10, 10, 400, 300),
                          backgroundColor: [1, 0, 0, 1],
                          appWindow: new __SpinningCubeWindow()});
  ui.createCanvasWindow({ title: "yellow",
                          rect: createRect(250, 50, 200, 400),
                          backgroundColor: [1, 1, 0, 1],
                          appWindow: new __InstanceOverrideWindow()});
  ui.createCanvasWindow({ title: "green",
                          rect: createRect(200, 300, 400, 300),
                          backgroundColor: [0, 1, 0, 1],
                          appWindow: new __SimpleViewerWindow(
    getURLFromPath('assets/cube.o3dtgz'))});
  var textWindow = new __ConsoleWindow();
  ui.createCanvasWindow({ title: "purple",
                          rect: createRect(100, 150, 400, 200),
                          backgroundColor: [1, 0, 1, 1],
                          appWindow: textWindow});

  textWindow.addText('\n\n' +
      'Welcome to the O3D window sample\n\n' +
      'This sample was created in about 2 days\n' +
      'to hopefully inspire possibilities.\n\n' +
      'It is not meant in anyway to be useful outside\n' +
      'of being a sample as the code has not been\n' +
      'designed to be reusable.');

  textWindow = new __ConsoleWindow();
  ui.createCanvasWindow({ title: "cyan",
                          rect: createRect(150, 330, 300, 200),
                          backgroundColor: [0, 1, 1, 1],
                          appWindow: textWindow});

  textWindow.addText('\n\n' +
      'Instructions:\n\n' +
      'Right-Click or Shift-Left-Click to\n' +
      'create a new random window.');
}
</script>
</head>
<body>
<!-- Start of o3d plugin -->
<div id="o3d" style="width:100%; height: 100%;"></div>
<!-- End of o3d plugin -->
<div style="display:none">
<textarea id="fx" name="fx" cols="80" rows="20">
  // The 4x4 world view projection matrix.
  float4x4 worldViewProjection : WORLDVIEWPROJECTION;

  // input parameters for our vertex shader
  struct a2v {
    float4 pos : POSITION;
    float4 col : COLOR;
  };

  // input parameters for our pixel shader
  // also the output parameters for our vertex shader
  struct v2f {
    float4 pos : POSITION;
    float4 col: COLOR;
  };

  /**
   * vsMain - our vertex shader for phong illumination
   *
   * @param IN.pos Position vector of vertex
   * @param IN.col Color of vertex
   */
  v2f vsMain(a2v IN) {
    /**
     * We transform each vertex by the view projection matrix to bring
     * it from world space to projection space.
     *
     * We return its color unchanged.
     */
    v2f OUT;

    OUT.pos = mul(IN.pos, worldViewProjection);
    OUT.col = IN.col;
    return OUT;
  }
  /**
   * psMain - pixel shader does nothing but return whatever color it was given.
   *
   * @param IN.pos Position vector of vertex
   * @param IN.col Color of vertex
   */
  float4 psMain(v2f IN): COLOR {
    return IN.col;
  }

  // Here we tell our effect file the functions
  // which specify our vertex and pixel shaders.

  // #o3d VertexShaderEntryPoint vsMain
  // #o3d PixelShaderEntryPoint psMain
// #o3d MatrixLoadOrder RowMajor
</textarea>
<textarea id="shadow" name="shadow" cols="80" rows="20">
  // The 4x4 world view projection matrix.
  float4x4 worldViewProjection : WORLDVIEWPROJECTION;

  // input parameters for our vertex shader
  struct a2v {
    float4 pos : POSITION;
    float4 col : COLOR;
  };

  // input parameters for our pixel shader
  // also the output parameters for our vertex shader
  struct v2f {
    float4 pos : POSITION;
    float4 col: COLOR;
  };

  /**
   * vsMain - our vertex shader for phong illumination
   *
   * @param IN.pos Position vector of vertex
   * @param IN.col Color of vertex
   */
  v2f vsMain(a2v IN) {
    /**
     * We transform each vertex by the view projection matrix to bring
     * it from world space to projection space.
     *
     * We return its color unchanged.
     */
    v2f OUT;

    OUT.pos = mul(IN.pos, worldViewProjection);
    OUT.col = IN.col;
    return OUT;
  }
  /**
   * psMain - pixel shader does nothing but return whatever color it was given.
   *
   * @param IN.pos Position vector of vertex
   * @param IN.col Color of vertex
   */
  float4 psMain(v2f IN): COLOR {
    return float4(0,0,0,IN.col.w * 0.5);
  }

  // Here we tell our effect file the functions
  // which specify our vertex and pixel shaders.

  // #o3d VertexShaderEntryPoint vsMain
  // #o3d PixelShaderEntryPoint psMain
  // #o3d MatrixLoadOrder RowMajor
</textarea>
<textarea id="instanceoverrideshader" name="fx" cols="80" rows="20">
// The 4x4 world view projection matrix.
float4x4 worldViewProjection : WorldViewProjection;
float4x4 worldInverseTranspose : WorldInverseTranspose;
float4x4 world : World;

// positions of the light and camera
float3 light_pos;
float3 camera_pos;

// lighting components of the light source
float4 light_ambient;
float4 light_diffuse;
float4 light_specular;

//  shininess of the material. (for specular lighting)
float shininess;

float4 colorMult;

// input parameters for our vertex shader
struct a2v {
  float4 pos : POSITION;
  float3 normal : NORMAL;
  float4 col : COLOR;
};

// input parameters for our pixel shader
// also the output parameters for our vertex shader
struct v2f {
  float4 pos : POSITION;
  float4 pos2 : TEXCOORD0;
  float3 norm : TEXCOORD1;
  float3 light : TEXCOORD2;
  float4 col : COLOR;
};

/**
 * vsMain - our vertex shader
 *
 * @param IN.pos Position vector of vertex
 * @param IN.normal Normal of vertex
 * @param IN.col Color of vertex
 */
v2f vsMain(a2v IN) {
  /**
   * We use the standard phong illumination equation here.
   * We restrict (clamp) the dot products so that we
   * don't get any negative values.
   * All vectors are normalized for proper calculations.
   *
   * The output color is the summation of the
   * ambient, diffuse, and specular contributions.
   *
   * Note that we have to transform each vertex and normal
   * by the view projection matrix first.
   */
  v2f OUT;

  OUT.pos = mul(IN.pos, worldViewProjection);
  OUT.pos2 = OUT.pos;
  OUT.norm = mul(float4(IN.normal, 0), worldInverseTranspose).xyz;
  OUT.light = light_pos - mul(IN.pos, world).xyz;
  OUT.col = IN.col;
  return OUT;
}
/**
 * psMain - pixel shader
 *
 * @param IN.pos Position vector of vertex
 * @param IN.col Color of vertex
 */
float4 psMain(v2f IN): COLOR {
  float3 light = normalize(IN.light);
  float3 normal = normalize(IN.norm);
  float3 litR = normalize(2 * dot(light, normal) * normal - light);
  float3 v = normalize(mul(float4(camera_pos, 1),
                           worldViewProjection).xyz - IN.pos2.xyz);

  // use lit function to calculate phong shading
  float4 phong_coeff = lit(dot(normal, light), dot(litR, v), shininess);
  float4 ambient = light_ambient * phong_coeff.x * IN.col;
  float4 diffuse = light_diffuse * phong_coeff.y * IN.col;
  float4 specular = light_specular * phong_coeff.z * IN.col;

  return (ambient + diffuse) * colorMult + specular;
}

// Here we tell our effect file *which* functions are
// our vertex and pixel shaders.

// #o3d VertexShaderEntryPoint vsMain
// #o3d PixelShaderEntryPoint psMain
// #o3d MatrixLoadOrder RowMajor
</textarea>
</div>
</body>
</html>
